#!/usr/bin/env python3
from __future__ import print_function
import xlrd
from datetime import datetime, date
import re
import sys
import os
from apostl import Start_Fill
import fill

if len(sys.argv) != 3:
    print('	{} [input.excel] [start-row-number]'.format(sys.argv[0]))
    sys.exit(-1)

start = int(sys.argv[2]) - 1
#total = int(sys.argv[3]) 
total = 1
#today = date.today().strftime('%Y-%m-%d') + '-apostl'
#dstdir = os.path.join("out", today)
#if not os.path.exists(dstdir):
#    os.makedirs(dstdir)

rb = xlrd.open_workbook(sys.argv[1])
sheet = rb.sheet_by_index(0)
rows = sheet.nrows
cols =  sheet.ncols
#0 empty 1 string 2 number 3 date
#for x in range(start, rows):
for x in range(start, start + total):
    shit = False
    dualtest = False
    test_date = ""
    test_date_month = 0
    test_date_day = 0
    test_date_year = 2020
    report_date  = ""
    report_date_month = 0
    report_date_day = 0
    report_date_year = 2020
    first_name, last_name = "", ""
    birth = ""
    gender = ""
    tel = ""
    email = ""
    licence  = ""
    address = ""
 
    city  = ""
    street = ""
    zipcode = ""
    state = ""
    start = ord('A')

    for y in range(cols):
        cell = sheet.cell(x, y)
      
        if y == (ord('G')  - start) and cell.ctype == 1:
            first_name = cell.value
            print(first_name , end="|")
        
        if y == (ord('I')  - start) and cell.ctype == 1:
            last_name = cell.value
            print(last_name , end="|")
       
        if y == (ord('J')  - start) and cell.ctype == 3:
            cell_tuple = xlrd.xldate_as_tuple(cell.value, datemode=0)
            birth = (date(*cell_tuple[:3]).strftime('%Y/%m/%d'))
            print(birth, end="|")

        if y == (ord('K')  - start) and cell.ctype == 1:
            gender = cell.value
            gender = gender.lower()
            print(gender, end="|")

        if y == (ord('L')  - start) and cell.ctype == 1:
            tel = cell.value
            print(tel, end="|")
        if y == (ord('L')  - start) and cell.ctype == 2:
            tmp = cell.value
            tel = "{:.0f}".format(tmp) 
            print(tel, end="|")

        if y == (ord('M')  - start) and cell.ctype == 1:
            email = cell.value
            print(email, end="|")
       
        if y == (ord('F')  - start) and cell.ctype == 1:
            licence = cell.value
            print(licence, end="|")
        if y == (ord('F')  - start) and cell.ctype == 2:
            licence = "{:.0f}".format(cell.value)
            print(licence, end="|")
       
        if y == (ord('P') - start) and cell.ctype ==1 :
            street = cell.value
            print(street, end="|")
            
        if y == (ord('Q') - start) and cell.ctype ==1 :
            city  = cell.value
            print(city, end="|")
      
        if y == (ord('R') - start) and cell.ctype ==1 :
            state  = cell.value
            print(state, end="|")
            
        if y == (ord('S')  - start):
            print(cell.ctype)
            tmp = cell.value
            zipcode = "{:.0f}".format(tmp) 
            print(zipcode)

   
    data_dict = {
   'last_name': last_name,  #first name
   'first_name': first_name,  #second name
   'dob': birth,
   'gender': 'M',
   'email_id': email,  #email
   'phone': tel, #phone
   'address': street,  #
   'city': city,  #city
   'state': state,  #state
   'zip_code': zipcode,
   'passport':licence
    }
    if gender == "female":
        data_dict['gender'] = 'F'
    d = date.today()
    name =  first_name + " " + last_name
    tmp_dict = {
               'Text_1': name,
               'Text_2': d.strftime("%m/%d 10:00"),
               }
    fill.write_fillable_pdf("a.pdf", name + "-" +email + " .pdf", tmp_dict)
    print(data_dict)
    Start_Fill(**data_dict)



